/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 * 工作单完成
 */
define(['N/record', 'N/search', 'N/https', 'N/ui/dialog',], function (record, search, https, dialog) {
    var woId;
    var isbackflush;
    var startoperation, startoperationText;
    var processTypeId;
    var componentLineCount;
    var totalReceiptQuantity;

    function pageInit(context) {
        context.currentRecord.getField('endoperation').isDisabled = true;//禁用结束操作
        context.currentRecord.getField('custbody_iscreatetransferinventory').isDisplay = false;//不显示'是否创建库转移单'
        isbackflush = context.currentRecord.getValue('isbackflush');
        componentLineCount = context.currentRecord.getLineCount('component');
        woId = context.currentRecord.getValue('createdfrom');
    }

    function validateField(context) {
        try {
            if (isbackflush) {//反冲
                if (totalReceiptQuantity) {
                    if (context.fieldId === 'completedquantity') {//完工数量
                        if (context.currentRecord.getValue('completedquantity') > totalReceiptQuantity) {
                            alert('[完工数量]不能大于收货数量:' + totalReceiptQuantity);
                            return false;
                        }
                    }
                    if (context.fieldId === 'custbody_reportwork_qty') {    //报工数量
                        if (context.currentRecord.getValue('custbody_reportwork_qty') != totalReceiptQuantity) {
                            alert('[报工数量]需要等于收货数量:' + totalReceiptQuantity);
                            return false;
                        }
                    }
                    if (context.fieldId === 'custbody_defectivequantity') {  //次品数量
                        var defectivequantity = context.currentRecord.getValue('custbody_defectivequantity');
                        defectivequantity = defectivequantity ? defectivequantity : 0;
                        if (defectivequantity > totalReceiptQuantity) {
                            alert('[次品数量]不能大于收货数量:' + totalReceiptQuantity);
                            return false;
                        } else if (defectivequantity < 0) {
                            alert('[次品数量]不能小于数量:0');
                            return false;
                        }
                        var completedquantity = context.currentRecord.getValue('completedquantity');
                        completedquantity = completedquantity ? completedquantity : 0;
                        var totalQuantity = parseInt(defectivequantity) + parseInt(completedquantity);
                        if (totalQuantity > totalReceiptQuantity) {
                            alert('[完工数量,次品数量]的总数量:' + totalQuantity + '不能大于收货数量:' + totalReceiptQuantity);
                            return false;
                        }
                    }
                    if (context.fieldId === 'custbody_processscrapquantity') { //废品数量
                        var processscrapquantity = context.currentRecord.getValue('custbody_processscrapquantity');
                        processscrapquantity = processscrapquantity ? processscrapquantity : 0;
                        if (processscrapquantity > totalReceiptQuantity) {
                            alert('[废品数量]不能大于收货数量:' + totalReceiptQuantity);
                            return false;
                        } else if (processscrapquantity < 0) {
                            alert('[废品数量]不能小于数量:0');
                            return false;
                        }
                        var defectivequantity = context.currentRecord.getValue('custbody_defectivequantity');
                        var completedquantity = context.currentRecord.getValue('completedquantity');
                        defectivequantity = defectivequantity ? defectivequantity : 0;
                        completedquantity = completedquantity ? completedquantity : 0;
                        var totalQuantity = parseInt(defectivequantity) + parseInt(processscrapquantity) + parseInt(completedquantity);
                        if (totalQuantity > totalReceiptQuantity) {
                            alert('[完工数量,次品数量,废品数量]的总数量:' + totalQuantity + '不能大于收货数量:' + totalReceiptQuantity);
                            return false;
                        }
                    }
                }
                //库位 实际完工库位
                if (context.fieldId === 'custbody_bin') {
                    var custbody_actual_issue_location = context.currentRecord.getValue('custbody_actual_issue_location');
                    var custbody_bin = context.currentRecord.getValue('custbody_bin');
                    var custbody_bin_text = context.currentRecord.getText('custbody_bin');
                    var location = context.currentRecord.getValue('location');

                    //判断是否已选择实际完工地点location
                    if (custbody_actual_issue_location === "" && custbody_bin !== "") {
                        alert('请先选择实际完工地点');
                        context.currentRecord.setValue("custbody_bin", "");
                        return false;
                    }

                    //判断地点中，是否包含该库位
                    var mySearch = search.create({
                        type: search.Type.BIN,
                        filters: [['location', 'is', custbody_actual_issue_location], 'and', ['binnumber', 'is', custbody_bin_text]],
                        columns: ['internalid']
                    });
                    var searchResult = mySearch.run().getRange({start: 0, end: 1});
                    if (searchResult.length <= 0) {
                        alert('此地点中不含有此库位，请维护或重新选择');
                        context.currentRecord.setValue("custbody_bin", "");
                        return false;
                    }
                    for (var i = 0; i < context.currentRecord.getLineCount('component'); i++) {
                        var itemId = context.currentRecord.getSublistValue('component', 'item', i);
                        log.debug("itemId", itemId);
                        try {
                            var itemRec = record.load({
                                type: 'inventoryitem',
                                id: itemId
                            });
                            // var responseBodyStr = JSON.stringify(itemRec);
                            // for (var qwe = 0; qwe < responseBodyStr.length; qwe = qwe + 3000) {
                            //     log.debug({
                            //         title: 'responseBodyStr' + qwe,
                            //         details: responseBodyStr.slice(qwe, qwe + 3000)
                            //     });
                            // }
                            var itemName = itemRec.getText('itemid');
                            log.debug('itemName', itemName);
                            var item_exist_bin_flag = false;
                            for (var j = 0; j < itemRec.getLineCount('binnumber'); j++) {
                                var binnumber = itemRec.getSublistValue('binnumber', 'binnumber', j);
                                if (binnumber === custbody_bin) {
                                    item_exist_bin_flag = true;
                                    //“实际完工地点”与“地点”不一致时,判断库存可用量
                                    if (location !== custbody_actual_issue_location) {
                                        //库存可用量
                                        var quantityavailable = itemRec.getSublistValue('binnumber', 'onhandavail', j);
                                        //报工数
                                        var custbody_reportwork_qty = context.currentRecord.getValue('custbody_reportwork_qty');
                                        //BOM数
                                        var bom_quantity = context.currentRecord.getSublistValue('component', 'quantityper', i);
                                        var temp_quantity = parseFloat((Number(custbody_reportwork_qty) * Number(bom_quantity)).toPrecision(12));
                                        log.debug("库存可用量", quantityavailable);
                                        log.debug("info", "报工数x单个BOM对应的组件数量：" + custbody_reportwork_qty + "*" + bom_quantity + "=" + temp_quantity);
                                        if (quantityavailable < temp_quantity) {
                                            alert("货品" + itemName + "在实际完工库位可用量为xx,小于（报工数x单个BOM对应的组件数量）");
                                            context.currentRecord.setValue("custbody_bin", "");
                                            return false;
                                        } else {
                                            //显示“是否创建转移库存单”复选框
                                            context.currentRecord.getField('custbody_iscreatetransferinventory').isDisplay = true;
                                        }

                                    }
                                }
                            }
                            if (!item_exist_bin_flag) {
                                alert('货品' + itemName + '不含有此库位，请维护');
                                return false;

                            }
                        } catch (e) {
                            //先搜索item类型再load，浪费系统资源，直接用异常处理比较快
                            if (e.name === "SSS_RECORD_TYPE_MISMATCH") {
                                log.debug("info", "不是库存商品，直接跳过");
                            } else {
                                log.debug("系统错误", e);
                            }
                        }
                    }

                }
            }
        } catch (e) {
        }
        return true;
    }

    function fieldChanged(context) {
        if (context.fieldId === 'startoperation') {//开始操作,结束操作  一致
            startoperation = context.currentRecord.getValue('startoperation');
            startoperationText = context.currentRecord.getText('startoperation');
            context.currentRecord.setValue('endoperation', startoperation);
        }
        if (isbackflush) {//反冲
            if (context.fieldId === 'startoperation' && startoperationText.length > 0) {//开始操作
                processTypeId = getProcessTypeIdByOperationTaskId(startoperation);
                if (processTypeId === '4') {//自制工序
                    dialog.alert({title: "提示", message: "自制工序无法完工及反冲耗料!"});
                    context.currentRecord.setText('startoperation', '');
                }
                if (processTypeId === '1' || processTypeId === '2' || processTypeId === '3') {//外协工序
                    handlePoItemReceiptQty(context);// 判断该工序对应的相关费用货品的采购单收货完成状况
                }
            }
            // if (context.fieldId === 'completedquantity') {//完工数量,报工数量一致
            //     context.currentRecord.setValue('custbody_reportwork_qty', context.currentRecord.getValue('completedquantity'));
            // }
            if (context.fieldId === 'custbody_reportwork_qty') {//报工数量
                for (var i = 0; i < componentLineCount; i++) {
                    var a = context.currentRecord.getText('startoperation');
                    var b = context.currentRecord.getSublistText('component', 'operationsequencenumber', i);
                    if (a == b) {
                        var itemId = context.currentRecord.getSublistValue('component', 'item', i);
                        var reportworkquantity = context.currentRecord.getValue('custbody_reportwork_qty');
                        if (itemId === '868' || itemId === '49' || itemId === '866') {
                            var quantityper = context.currentRecord.getSublistValue('component', 'quantityper', i);
                            context.currentRecord.selectLine('component', i);
                            context.currentRecord.setCurrentSublistValue('component', 'quantity', parseFloat(quantityper * reportworkquantity));
                            context.currentRecord.commitLine('component');
                        }
                    }
                }
            }
            if (context.fieldId === 'custbody_processscrapquantity') {//废品数量
                var linequantity = Number(context.currentRecord.getValue('completedquantity')) + Number(context.currentRecord.getValue('custbody_processscrapquantity'))
                for (var i = 0; i < componentLineCount; i++) {
                    var a = context.currentRecord.getText('startoperation');
                    var b = context.currentRecord.getSublistText('component', 'operationsequencenumber', i);
                    if (a == b) {
                        var itemId = context.currentRecord.getSublistValue('component', 'item', i);
                        if (itemId !== '868' && itemId !== '49' && itemId !== '866') {
                            var quantityper = context.currentRecord.getSublistValue('component', 'quantityper', i);
                            context.currentRecord.selectLine('component', i);
                            context.currentRecord.setCurrentSublistValue('component', 'quantity', parseFloat(quantityper * linequantity));
                            context.currentRecord.commitLine('component');
                        }
                    }
                }
            }

            if (context.fieldId === 'custbody_actual_issue_location') {
                if (context.currentRecord.getValue('custbody_actual_issue_location') === '') {
                    return;
                }
                var startOperation = context.currentRecord.getValue('startoperation');
                if (startOperation === '') {
                    dialog.alert({title: "提示", message: "请先选择开始操作！"});
                    context.currentRecord.setValue('custbody_actual_issue_location', '');
                    return;
                }
            }


            if (context.fieldId === 'custbody_bin') {
                var locationId = context.currentRecord.getValue('custbody_actual_issue_location');
                var location = context.currentRecord.getValue('location');
                var binId = context.currentRecord.getValue('custbody_bin');
                var displayFlag = false;
                if (locationId && binId && location !== locationId && processTypeId !== '4') {
                    for (var i = 0; i < componentLineCount; i++) {
                        startoperationText = context.currentRecord.getText('startoperation');
                        var operationsequencenumber = context.currentRecord.getSublistValue('component', 'operationsequencenumber', i);
                        if (startoperationText === operationsequencenumber) {
                            var itemId = context.currentRecord.getSublistValue('component', 'item', i);
                            if (itemId !== '868' && itemId !== '49' && itemId !== '866') {
                                try {
                                    var results = search.create({
                                        type: 'item',
                                        columns: ['type'],
                                        filters: [['internalid', 'is', itemId]]
                                    }).run().getRange(0, 1);
                                    var rec = record.load({type: results[0].recordType, id: itemId});
                                    var itemName = rec.getValue('itemid');
                                    for (var j = 0; j < rec.getLineCount('binnumber'); j++) {
                                        var invtlocationId = rec.getSublistValue('binnumber', 'location', j);
                                        var invtbinId = rec.getSublistValue('binnumber', 'binnumber', j);
                                        if (invtlocationId === locationId && invtbinId === binId) {
                                            var onhandavail = rec.getSublistValue('binnumber', 'onhandavail', j);
                                            var binnumerName = rec.getSublistText('binnumber', 'binnumber', j);
                                            var locationName = rec.getSublistText('binnumber', 'location', j);
                                            var itemQuantity = Number(context.currentRecord.getSublistValue('component', 'quantity', i));
                                            if (onhandavail >= itemQuantity) {
                                                dialog.alert({
                                                    title: "提示",
                                                    message: "请创建库存转移单!货品[" + itemName + "],库存可用数量为" + onhandavail + "!"
                                                });
                                                displayFlag = true;
                                            } else {
                                                dialog.alert({
                                                    title: "提示",
                                                    message: "货品[" + itemName + "],库存可用数量为" + onhandavail + ",小于货品行数量" + itemQuantity + "!"
                                                });
                                                displayFlag = false;
                                            }
                                        }
                                    }
                                } catch (e) {
                                }
                            }
                        }
                    }
                }
                //控制显示是否创建库存转移单
                if (displayFlag) {
                    context.currentRecord.getField('custbody_iscreatetransferinventory').isDisplay = true;
                } else {
                    context.currentRecord.getField('custbody_iscreatetransferinventory').isDisplay = false;
                }
            }
            //是否创建库存转移单
            if (context.fieldId === 'custbody_iscreatetransferinventory') {
                var relatedTranfer = context.currentRecord.getValue('custbody_related_itid');
                //勾选
                if (context.currentRecord.getValue('custbody_iscreatetransferinventory')) {
                    //前置判断
                    //判断此工序对应的每一个非费用组件是否含有“线边仓-生产区”库位，若不含有则弹框提示“xxx货品不含有线边仓-生产区库位，请维护“并把复选框清空
                    for (var x = 0; x < context.currentRecord.getLineCount('component'); x++) {
                        var itemId1 = context.currentRecord.getSublistValue('component', 'item', x);
                        log.debug("itemId1", itemId1);
                        try {
                            var itemRec1 = record.load({
                                type: 'inventoryitem',
                                id: itemId1
                            });
                            var itemName1 = itemRec1.getText('itemid');
                            log.debug('itemName1', itemName1);
                            var item_exist_bin_flag = false;
                            var xbc_bin = "3";
                            for (var y = 0; y < itemRec1.getLineCount('binnumber'); y++) {
                                var binnumber = itemRec1.getSublistValue('binnumber', 'binnumber', y);
                                if (binnumber.toString() === xbc_bin) {
                                    item_exist_bin_flag = true;
                                }
                            }
                            if (!item_exist_bin_flag) {
                                alert(itemName1 + '货品不含有线边仓-生产区库位，请维护');
                                context.currentRecord.setValue('custbody_iscreatetransferinventory', false);
                                return false;
                            }
                        } catch (e) {
                            //先搜索item类型再load，浪费系统资源，直接用异常处理比较快
                            if (e.name === "SSS_RECORD_TYPE_MISMATCH") {
                                log.debug("info", "不是库存商品，直接跳过");
                            } else {
                                log.debug("系统错误", e);
                            }
                        }
                    }
                    //若没有相关库存转移单,则创建
                    if (relatedTranfer.length > 0) {
                        dialog.alert({title: "提示", message: "请勿重复创建库存转移单!"});
                    } else {
                        var param = getCreateInventoryTransferParam(context);
                        log.debug("param", param);
                        var response = https.get({url: '/app/site/hosting/scriptlet.nl?script=126&deploy=1&param=' + param});
                        log.debug("response.body", response.body);
                        var messages = JSON.parse(response.body).messages;
                        dialog.alert({title: "提示", message: JSON.stringify(messages)});
                        if (messages[0].success) {
                            context.currentRecord.setValue('custbody_related_itid', messages[0].id); //回写创建好的 库存转移单id
                        } else {
                            return;
                        }
                        var related = context.currentRecord.getText('custbody_related_itid');
                        if (related.length > 0) {
                        } else {
                            context.currentRecord.setValue('custbody_iscreatetransferinventory', false);
                        }
                    }
                }
            }
        } else if (!isbackflush) { //工单完成
            if (context.fieldId === 'startoperation' && startoperationText.length > 0) {
                processTypeId = getProcessTypeIdByOperationTaskId(startoperation);
                if (processTypeId === '1' || processTypeId === '2' || processTypeId === '3') {
                    dialog.alert({title: "提示", message: "委外工序不能直接完工回报!"});
                    context.currentRecord.setText('startoperation', '');
                } else {
                    //上道工序的完工数量之和 - 本道工序的报工数量之和,  >0,填在完工数量,<=0 alert,setText
                    var processNum = Number(startoperationText);
                    if (processNum !== 1) {
                        //获得上道工序完工数量
                        var operationseqLineNumber = context.currentRecord.findSublistLineWithValue('operation', 'operationsequence', processNum);
                        var predecessorCompletedquantity = context.currentRecord.getSublistValue('operation', 'predecessorcompletedquantity', operationseqLineNumber);
                        //查找当前工序报工数量
                        var currentReportquantity = 0;
                        search.create({
                            type: 'workordercompletion',
                            columns: ['custbody_reportwork_qty'],
                            filters: [['createdfrom', 'is', woId], 'and', ['mainline', 'is', 'T'], 'and', ['custbody_wo_processnum', 'equalto', processNum]]
                        }).run().each(function (result) {
                            currentReportquantity += Number(result.getValue('custbody_reportwork_qty'));
                            return true;
                        });
                        var subtractQuantity = predecessorCompletedquantity - currentReportquantity;
                        // var subtractQuantity = getSubtractQuantity(startoperationText);
                        if (subtractQuantity > 0) {
                            dialog.alert({
                                title: "提示",
                                message: '上道工序总完工数量:' + predecessorCompletedquantity + ',本道工序总报工数量:' + currentReportquantity
                            });
                            context.currentRecord.setValue('completedquantity', subtractQuantity);
                        } else {
                            dialog.alert({
                                title: "提示",
                                message: '无法完工!上道工序总完工数量:' + predecessorCompletedquantity + ',本道工序总报工数量:' + currentReportquantity
                            });
                            context.currentRecord.setText('startoperation', '');
                        }

                    }
                }
            }
        }
    }

    function saveRecord(context) {
        try {
            if (isbackflush) {//工单完成及反冲
                if (totalReceiptQuantity) {
                    var processscrapquantity = context.currentRecord.getValue('custbody_processscrapquantity');
                    var defectivequantity = context.currentRecord.getValue('custbody_defectivequantity');
                    var completedquantity = context.currentRecord.getValue('completedquantity');
                    defectivequantity = defectivequantity ? defectivequantity : 0;
                    completedquantity = completedquantity ? completedquantity : 0;
                    processscrapquantity = processscrapquantity ? processscrapquantity : 0;
                    var totalQuantity = parseInt(defectivequantity) + parseInt(processscrapquantity) + parseInt(completedquantity);
                    if (totalQuantity != totalReceiptQuantity) {
                        dialog.alert({
                            title: "提示",
                            message: '[完工数量,次品数量,废品数量]的总数量:' + totalQuantity + '不等于收货数量:' + totalReceiptQuantity
                        });
                        return false;
                    }
                }
                for (var i = 0; i < componentLineCount; i++) {
                    if (context.currentRecord.getText('startoperation') == context.currentRecord.getSublistText('component', 'operationsequencenumber', i)) {
                        var itemId = context.currentRecord.getSublistValue('component', 'item', i);
                        if (itemId !== '868' && itemId !== '49' && itemId !== '866') {
                            var location = context.currentRecord.getText('location');
                            var actuallocation = context.currentRecord.getText('custbody_actual_issue_location');
                            var relatedtransation = context.currentRecord.getText('custbody_related_itid');
                            if (location !== actuallocation && relatedtransation.length === 0) {
                                dialog.alert({title: "提示", message: "[实际完工地点]和[地点]不一致,需转移耗料!"});
                                return false;
                            }
                        }
                    }
                }
            } else if (!isbackflush) {
                var reportwork_qty = context.currentRecord.getValue('custbody_reportwork_qty');
                if (!reportwork_qty || reportwork_qty <= 0) {
                    dialog.alert({title: "提示", message: "报工数量不能小于0!"});
                    return false;
                }
            }
            // dialog.alert({title: "提示", message: "正在处理,请稍候!"});
            //工作完成单,都要获得总账影响标志  和 设置工作单完成号
            var results = search.create({
                type: 'workordercompletion',
                columns: [],
                filters: [['createdfrom', 'is', woId],
                    'and', ['mainline', 'is', 'T'],
                    'and', ['custbody_wo_processnum', 'equalto', Number(context.currentRecord.getText('startoperation'))]]
            }).run().getRange(0, 100);
            context.currentRecord.setValue('custbody_identify', results.length + 1);
            context.currentRecord.setValue('custbody_wo_processnum', Number(context.currentRecord.getText('startoperation')).toString());
        } catch (e) {
        }
        return true;
    }

    function getProcessTypeIdByOperationTaskId(operationTaskId) {
        var fieldLookUp1 = search.lookupFields({
            type: 'manufacturingoperationtask',
            id: operationTaskId,
            columns: ['manufacturingcosttemplate']
        });
        var manufacturingcosttemplateId = fieldLookUp1.manufacturingcosttemplate[0].value;
        var fieldLookUp2 = search.lookupFields({
            type: 'manufacturingcosttemplate',
            id: manufacturingcosttemplateId,
            columns: ['custrecord_processtype']
        });
        return fieldLookUp2.custrecord_processtype[0].value;
    }

    function handlePoItemReceiptQty(context) {
        totalReceiptQuantity = 0;
        for (var i = 0; i < componentLineCount; i++) {
            var itemId = context.currentRecord.getSublistValue('component', 'item', i);
            if (itemId === '868' || itemId === '49' || itemId === '866') {
                startoperationText = context.currentRecord.getText('startoperation');
                var operationsequencenumber = context.currentRecord.getSublistValue('component', 'operationsequencenumber', i);
                if (operationsequencenumber === startoperationText) {
                    var poId, poTranid;  //查找该工序的费用货品对应的采购订单Id
                    search.create({
                        type: 'purchaseorder',
                        columns: ['tranid'],
                        filters: [['createdfrom', 'is', woId], 'and', ['anylineitem', 'is', itemId]]
                    }).run().each(function (result) {
                        poId = result.id;
                        poTranid = result.getValue('tranid');
                        if (poId) {  //如果有采购订单  则查找采购订单对应的  未勾选“倒冲加工费” 收货单
                            var itemReceiptSearch = search.create({
                                type: 'itemreceipt',
                                columns: [],
                                filters: [['createdfrom', 'is', poId],
                                    'and', ['mainline', 'is', 'T'],
                                    'and', ['custbody_backflushprocessingfee', 'is', 'F']]
                            });
                            var results = itemReceiptSearch.run().getRange(0, 100);
                            if (results.length > 0) {
                                for (var i = 0; i < results.length; i++) {
                                    var itemReceiptId = results[i].id;
                                    if (itemReceiptId) {
                                        var itemReceiptRec = record.load({
                                            type: 'itemreceipt',
                                            id: itemReceiptId,
                                            isDynamic: true
                                        });
                                        for (var j = 0; j < itemReceiptRec.getLineCount('item'); j++) {
                                            totalReceiptQuantity += itemReceiptRec.getSublistValue('item', 'quantity', j);
                                        }
                                    }
                                }
                                dialog.alert({
                                    title: "提示",
                                    message: '采购订单:' + poTranid + '存在未核销的收货单,收货数量:' + totalReceiptQuantity + '!'
                                });
                                context.currentRecord.setValue('completedquantity', totalReceiptQuantity);
                                context.currentRecord.setValue('custbody_reportwork_qty', totalReceiptQuantity);

                            } else {
                                dialog.alert({title: "提示", message: '采购订单:' + poTranid + '不存在未核销的收货单!'});
                                context.currentRecord.setText('startoperation', '');
                            }
                        } else {
                            dialog.alert({title: "提示", message: "采购订单不存在!"});
                            context.currentRecord.setText('startoperation', '');
                        }
                    });
                }
            }
        }
    }

    function getCreateInventoryTransferParam(context) {
        var transferItem = [];
        for (var i = 0; i < context.currentRecord.getLineCount('component'); i++) {
            var operation = context.currentRecord.getSublistValue('component', 'operationsequencenumber', i);
            if (startoperationText.length > 0 && operation === startoperationText) {
                var itemId = context.currentRecord.getSublistValue('component', 'item', i);
                //库存转移单  非委外加工费 货品
                if (itemId !== '868' && itemId !== '49' && itemId !== '866') {
                    var quantityper = context.currentRecord.getSublistValue('component', 'quantityper', i);
                    var reportquantity = context.currentRecord.getValue('custbody_reportwork_qty');
                    var item = {};
                    item.itemId = itemId;
                    item.quantity = quantityper * reportquantity;
                    item.fromLocation = context.currentRecord.getValue('custbody_actual_issue_location');
                    item.fromLocationName = context.currentRecord.getText('custbody_actual_issue_location');
                    item.fromBin = context.currentRecord.getValue('custbody_bin');
                    item.toBin = 3;
                    transferItem.push(item);
                }
            }
        }
        var items = transferItem;
        var param = {};
        param.type = 'inventorytransfer';
        param.related = woId;
        param.subsidiary = context.currentRecord.getValue('subsidiary');
        var fromLocations = [];
        fromLocations.push(context.currentRecord.getValue('custbody_actual_issue_location'));
        param.fromLocations = fromLocations;
        param.toLocation = context.currentRecord.getValue('location');
        // param.fromBin = context.currentRecord.getValue('custbody_bin');
        // param.toBin = 3;
        param.items = items;
        param = JSON.stringify(param);
        return param;
    }

    return {
        pageInit: pageInit,
        validateField: validateField,
        fieldChanged: fieldChanged,
        saveRecord: saveRecord
    };

});